﻿// 在 http://fsharp.net 上了解有关 F# 的更多信息
// 请参阅“F# 教程”项目以获取更多帮助。
open System
open System.Net
open System.IO;
open System.Threading;;
open Microsoft.FSharp.Control.WebExtensions
[<EntryPoint>]
let main argv = 
    printfn "%A" argv

    let getHtml url =
        async { let req = WebRequest.Create(Uri url)
                use! resp = req.AsyncGetResponse()
                use stream = resp.GetResponseStream()
                use reader = new StreamReader(stream,Text.Encoding.UTF8)
                let contents = reader.ReadToEnd()
                printfn "%A" Thread.CurrentThread.IsBackground
                return contents }

    let sites = ["http://www.bing.com";
                 "http://www.google.com";
                 "http://www.yahoo.com";
                 "http://www.search.com"]

    let htmlOfSites =
       Async.Parallel [for site in sites -> getHtml site]
    |>Async.RunSynchronously

    let savePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),Guid.NewGuid().ToString()+".txt")

    File.WriteAllText(savePath,"",Text.Encoding.UTF8)

    for i in htmlOfSites do 
       File.AppendAllText(savePath,"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"+i,Text.Encoding.UTF8)
    ignore(Console.ReadKey())
    0 // 返回整数退出代码


    //http://blog.csdn.net/fsharp_net/article/details/7094540